<?php
load_class('TMIS_TableDataGateway');
class Model_Sample_Ruku extends TMIS_TableDataGateway {
    var $tableName     = 'sample_ruku';
    var $primaryKey    = 'id';
    var $primaryName   = 'rukuCode';
    var $needCreateLog = true;// 需要打日志则加此参数
    var $codeField     = 'rukuCode';// 编号字段
    var $moduleName    = '样品间入库';// 模块名称

    var $hasMany = array (
        array(
            'tableClass'  => 'Model_Sample_Ruku2Product',
            'foreignKey'  => 'rukuId',
            'mappingName' => 'Products',
            'sort'        => 'id'
        )
    );

    /**
     * 新条码号
     * Time：2019/04/18 10:12:23
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    public function createNewCode($row = array() ,$nextNo = 0){
		$this->force_master = true;
        $code = 'code';
        //获取产品的编号：head使用产品编号：编号再产品编号后面+序号
        if($row['productId']){
            $modelProduct = FLEA::getSingleton('Model_Jichu_Product');
            $info = $modelProduct->find($row['productId']);
            $pre = $info['prefix'] ? $info['prefix'] : '';
        }
        if(!$pre){
            $pre = 'A' .date('ymd');
        }
        // 查找最后的条码
        $sql="SELECT {$code},append from sample_ruku2product where {$code} like '{$pre}%' order by append desc limit 0,1";
        $_r = $this->findBySql($sql);
        $row = $_r[0];
        if(empty($row[$code])){
            return $pre;
        }else{
            $pre = $pre .'-';
            $sql = "SELECT {$code},append from sample_ruku2product where {$code} like '{$pre}%' order by append desc limit 0,1";
            $_r = $this->findBySql($sql);
            $row = $_r[0];
            //自增1
            $max = $row['append'];
            return $pre .substr(11+$max+$nextNo,1);
        }

        // $pre = substr($row[$code],0,-4);
        return $pre .substr(11+$max+$nextNo,1);
    }

    public function getTypeSelect() {
        $sql = "SELECT distinct type from `sample_ruku2product` where type<>'' limit 0,100";
        $list = $this->findBySql($sql);

        //变成一级数组
        $type = array_col_values($list ,'type');

        //处理成可选项
        foreach ($type as $key => $v) {
            $optionsArr[] = array('text'=>$v,'value'=>$v);
        }

        return $optionsArr;
    }

    public function getTypeValues() {
        $sql = "SELECT distinct type from `sample_ruku2product` where type<>'' limit 0,100";
        $list = $this->findBySql($sql);

        //变成一级数组
        $type = array_col_values($list ,'type');

        //处理成可选项
        $optionsArr = array();
        foreach ($type as $key => $v) {
            $optionsArr[] = $v;
        }

        return $optionsArr;
    }

    //获取用途信息
    function getOptionsFiled($field){
        $sql = "SELECT distinct {$field} as showText from `sample_ruku` where {$field}<>'' limit 0,100";
        $list = $this->findBySql($sql);
        //变成一级数组
        $type = array_col_values($list ,'showText');

        //处理成可选项
        $optionsArr = array();
        foreach ($type as $key => $v) {
            $optionsArr[] = array('text'=>$v,'value'=>$v);
        }

        return $optionsArr;
    }

    //获取用途信息
    function getOptionsSonFiled($field){
        $sql = "SELECT distinct {$field} as showText from `sample_ruku2product` where {$field}<>'' order by {$field} asc";
        $list = $this->findBySql($sql);
        //变成一级数组
        $type = array_col_values($list ,'showText');

        //处理成可选项
        $optionsArr = array();
        foreach ($type as $key => $v) {
            $optionsArr[] = array('text'=>$v,'value'=>$v);
        }

        return $optionsArr;
    }

    //获取数据
    function getValuesFiled($field){
        $sql = "SELECT distinct {$field} as showText from `sample_ruku` where {$field}<>'' limit 0,100";
        $list = $this->findBySql($sql);
        //变成一级数组
        $type = array_col_values($list ,'showText');

        //处理成可选项
        $optionsArr = array();
        foreach ($type as $key => $v) {
            $optionsArr[] = $v;
        }

        return $optionsArr;
    }

}


?>